using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VWTTUF
    {
        public DataRow Convert2WTTUF(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryWTTUF(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["WTTMANDANT"] = " ".PadRight(2 ); 
         dwReturn["WTTKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["WTTWERK"] = " ".PadRight(3 ); 
         dwReturn["WTTABT"] = " ".PadRight(3 ); 
         dwReturn["WTTART"] = " ".PadRight(20 ); 
         dwReturn["WTTVAR"] = " ".PadRight(2 ); 
         dwReturn["WTTAENZL"] = " ".PadRight(0 ); 
         dwReturn["WTTAENB"] = " ".PadRight(8 ); 
         dwReturn["WTTAEND"] = " ".PadRight(0 ); 
         dwReturn["WTTNEUB"] = " ".PadRight(8 ); 
         dwReturn["WTTNEUD"] = " ".PadRight(0 ); 
         dwReturn["WTTEIL"] = " ".PadRight(0 ); 
         dwReturn["WTTNABRT"] = " ".PadRight(0 ); 
         dwReturn["WTTHGAQM"] = " ".PadRight(0 ); 
         dwReturn["WTTIGAQM"] = " ".PadRight(0 ); 
         dwReturn["WTTIFEU"] = " ".PadRight(0 ); 
         dwReturn["WTTPROBE"] = " ".PadRight(0 ); 
         dwReturn["WTTNFLRH"] = " ".PadRight(0 ); 
         dwReturn["WTTNFLRA"] = " ".PadRight(3 ); 
         dwReturn["WTTMFLRH"] = " ".PadRight(0 ); 
         dwReturn["WTTMFLRA"] = " ".PadRight(3 ); 
         dwReturn["WTTHFLRH"] = " ".PadRight(0 ); 
         dwReturn["WTTHFLRA"] = " ".PadRight(3 ); 
         dwReturn["WTTFILLER"] = " ".PadRight(47 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapWTTUF(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveWTTUF(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryWTTUF(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapWTTUF(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertWTTUF(dwLocal, ITXDBVERSION);
            }
            UpdateWTTUF(dwLocal);
        }
        public void InsertWTTUF(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO WTTUF(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryWTTUF(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM WTTUF WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM WTTUF  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "WTTUF";
            Return = Result;
            return Return;
        }
        public void DeleteWTTUF(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM WTTUF   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateWTTUF(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE WTTUF SET "
                    +"WTTMANDANT= :WTTMANDANT,"
                    +"WTTKUNDNR= :WTTKUNDNR,"
                    +"WTTWERK= :WTTWERK,"
                    +"WTTABT= :WTTABT,"
                    +"WTTART= :WTTART,"
                    +"WTTVAR= :WTTVAR,"
                    +"WTTAENZL= :WTTAENZL,"
                    +"WTTAENB= :WTTAENB,"
                    +"WTTAEND= :WTTAEND,"
                    +"WTTNEUB= :WTTNEUB,"
                    +"WTTNEUD= :WTTNEUD,"
                    +"WTTEIL= :WTTEIL,"
                    +"WTTNABRT= :WTTNABRT,"
                    +"WTTHGAQM= :WTTHGAQM,"
                    +"WTTIGAQM= :WTTIGAQM,"
                    +"WTTIFEU= :WTTIFEU,"
                    +"WTTPROBE= :WTTPROBE,"
                    +"WTTNFLRH= :WTTNFLRH,"
                    +"WTTNFLRA= :WTTNFLRA,"
                    +"WTTMFLRH= :WTTMFLRH,"
                    +"WTTMFLRA= :WTTMFLRA,"
                    +"WTTHFLRH= :WTTHFLRH,"
                    +"WTTHFLRA= :WTTHFLRA,"
                    +"WTTFILLER= :WTTFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("WTTMANDANT", OracleType.NVarChar).Value = dwData["WTTMANDANT"].ToString(); 
          or1.Parameters.Add("WTTKUNDNR", OracleType.NVarChar).Value = dwData["WTTKUNDNR"].ToString(); 
          or1.Parameters.Add("WTTWERK", OracleType.NVarChar).Value = dwData["WTTWERK"].ToString(); 
          or1.Parameters.Add("WTTABT", OracleType.NVarChar).Value = dwData["WTTABT"].ToString(); 
          or1.Parameters.Add("WTTART", OracleType.NVarChar).Value = dwData["WTTART"].ToString(); 
          or1.Parameters.Add("WTTVAR", OracleType.NVarChar).Value = dwData["WTTVAR"].ToString(); 
          or1.Parameters.Add("WTTAENZL", OracleType.NVarChar).Value = dwData["WTTAENZL"].ToString(); 
          or1.Parameters.Add("WTTAENB", OracleType.NVarChar).Value = dwData["WTTAENB"].ToString(); 
          or1.Parameters.Add("WTTAEND", OracleType.NVarChar).Value = dwData["WTTAEND"].ToString(); 
          or1.Parameters.Add("WTTNEUB", OracleType.NVarChar).Value = dwData["WTTNEUB"].ToString(); 
          or1.Parameters.Add("WTTNEUD", OracleType.NVarChar).Value = dwData["WTTNEUD"].ToString(); 
          or1.Parameters.Add("WTTEIL", OracleType.NVarChar).Value = dwData["WTTEIL"].ToString(); 
          or1.Parameters.Add("WTTNABRT", OracleType.NVarChar).Value = dwData["WTTNABRT"].ToString(); 
          or1.Parameters.Add("WTTHGAQM", OracleType.NVarChar).Value = dwData["WTTHGAQM"].ToString(); 
          or1.Parameters.Add("WTTIGAQM", OracleType.NVarChar).Value = dwData["WTTIGAQM"].ToString(); 
          or1.Parameters.Add("WTTIFEU", OracleType.NVarChar).Value = dwData["WTTIFEU"].ToString(); 
          or1.Parameters.Add("WTTPROBE", OracleType.NVarChar).Value = dwData["WTTPROBE"].ToString(); 
          or1.Parameters.Add("WTTNFLRH", OracleType.NVarChar).Value = dwData["WTTNFLRH"].ToString(); 
          or1.Parameters.Add("WTTNFLRA", OracleType.NVarChar).Value = dwData["WTTNFLRA"].ToString(); 
          or1.Parameters.Add("WTTMFLRH", OracleType.NVarChar).Value = dwData["WTTMFLRH"].ToString(); 
          or1.Parameters.Add("WTTMFLRA", OracleType.NVarChar).Value = dwData["WTTMFLRA"].ToString(); 
          or1.Parameters.Add("WTTHFLRH", OracleType.NVarChar).Value = dwData["WTTHFLRH"].ToString(); 
          or1.Parameters.Add("WTTHFLRA", OracleType.NVarChar).Value = dwData["WTTHFLRA"].ToString(); 
          or1.Parameters.Add("WTTFILLER", OracleType.NVarChar).Value = dwData["WTTFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
